package com.boat.framework.base.exception;

/**
 * 统一抛出的异常
 * 
 * @author 周江
 *
 */
public class ServiceException extends RuntimeException {
	private static final long serialVersionUID = 8805532382863400999L;

	private String code;
	private String message;

	public ServiceException() {
		super();
	}

	public ServiceException(ErrorCode errorCode) {
		super();
		init(errorCode, null);
	}

	public ServiceException(ErrorCode errorCode, String params[]) {
		super();
		init(errorCode, params);
	}

	public ServiceException(ErrorCode errorCode, String params[], Throwable cause) {
		super(cause);
		init(errorCode, params);
	}

	public ServiceException(String code, String message) {
		super();
		this.code = code;
		this.message = message;
	}
	
	public ServiceException(String message) {
		super();
		this.code = CommonErrorCode.SYSTEM_ERROR.getCode();
		this.message = message;
	}

	public String getCode() {
		return code;
	}
	
	@Override
	public String getMessage() {
		return message;
	}

	private void init(ErrorCode errorCode, Object params[]) {
		this.code = null == errorCode ? "" : errorCode.getCode();
		this.message = null == errorCode ? "" : errorCode.getMessage(params);
	}
}
